﻿Imports Capa_de_Acceso_a_Datos
Public Class frmSupervisarConsultas
    Private consultas As DataTable
    Public farmaciaId As Integer = 0
    Private iniciando As Boolean

#Region "Inicializaciones y carga de formulario"

    Private Sub SupervisarConsultas_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmMenuAuditoria.Visible = True
        Me.Dispose()
    End Sub

    Private Sub Supervisar_Consultas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        iniciando = True
        cargarCombos()
        cargarConsultas()
        iniciando = False
    End Sub

    Private Sub cargarCombos()
        cargarComboEmpleadosCafapro()
        cargarComboEstadosConsultas()
        cargarComboTiposConsultas()
    End Sub

    Private Sub cargarComboEstadosConsultas()
        cboEstadoConsulta.DataSource = EstadoConsultaDAO.comboEstadoConsulta
        cboEstadoConsulta.DisplayMember = "Descripcion"
        cboEstadoConsulta.ValueMember = "EstadoConsultaId"
        cboEstadoConsulta.SelectedValue = 0
    End Sub

    Private Sub cargarComboTiposConsultas()
        cboTipoConsulta.DataSource = TipoConsultaDAO.comboTiposConsultas
        cboTipoConsulta.DisplayMember = "Descripcion"
        cboTipoConsulta.ValueMember = "TipoConsultaId"
        cboTipoConsulta.SelectedValue = 0
    End Sub

    Private Sub cargarComboEmpleadosCafapro()
        cboEmpleadoCafapro.DataSource = EmpleadoDAO.comboCalendarioEmpleados
        cboEmpleadoCafapro.DisplayMember = "Descripcion"
        cboEmpleadoCafapro.ValueMember = "EmpleadoId"
        cboEmpleadoCafapro.SelectedValue = 0
    End Sub

    Private Sub cargarConsultas()
        consultas = ConsultaFarmaciaDAO.obtenerConsultasFarmaciasCalendario(farmaciaId, cboEstadoConsulta.SelectedValue, cboTipoConsulta.SelectedValue, cboEmpleadoCafapro.SelectedValue)

        'Definimos la correspondencia del DataSource con los campos del calendario
        calendarioConsultas.Storage = New DevExpress.XtraScheduler.SchedulerStorage()
        calendarioConsultas.Storage.Appointments.DataSource = consultas
        calendarioConsultas.Storage.Appointments.DataMember = "ConsultaFarmaciaId"
        calendarioConsultas.Storage.Appointments.Mappings.Label = "EstadoConsultaId"
        'calendarioConsultas.Storage.Appointments.Mappings.Description = "ConsultaFarmaciaMotivo"
        calendarioConsultas.Storage.Appointments.Mappings.Start = "ConsultaFarmaciaFecha"
        calendarioConsultas.Storage.Appointments.Mappings.End = "ConsultaFarmaciaFechaFin"
        'calendarioConsultas.Storage.Appointments.Mappings.Status = "EstadoConsultaId"
        calendarioConsultas.Storage.Appointments.Mappings.Subject = "FarmaciaNombre"


        'Definimos los colores de los estados y los labels
        calendarioConsultas.Storage.Appointments.Labels.Clear()
        calendarioConsultas.Storage.Appointments.Labels.Add(Color.Yellow, "Sin Seleccionar")
        calendarioConsultas.Storage.Appointments.Labels.Add(Color.GreenYellow, "Solucionada")
        calendarioConsultas.Storage.Appointments.Labels.Add(Color.Red, "No Solucionada")

    End Sub

#End Region

    Private Sub actualizarCalendario()
        consultas = ConsultaFarmaciaDAO.obtenerConsultasFarmaciasCalendario(farmaciaId, cboEstadoConsulta.SelectedValue, cboTipoConsulta.SelectedValue, cboEmpleadoCafapro.SelectedValue)
        calendarioConsultas.Storage.Appointments.DataSource = consultas
        calendarioConsultas.Storage.RefreshData()
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        'farmaciaId = frmSeleccionarFarmacia.ShowDialog()
        If farmaciaId <> -1 Then
            actualizarCalendario()
        End If
    End Sub

    Private Sub cboEmpleadoCafapro_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEmpleadoCafapro.SelectedIndexChanged
        If Not iniciando Then
            actualizarCalendario()
        End If
    End Sub

    Private Sub cboEstadoConsulta_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEstadoConsulta.SelectedIndexChanged
        If Not iniciando Then
            actualizarCalendario()
        End If
    End Sub

    Private Sub cboTipoConsulta_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTipoConsulta.SelectedIndexChanged
        If Not iniciando Then
            actualizarCalendario()
        End If
    End Sub

    Private Sub btnDiario_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiario.Click
        calendarioConsultas.ActiveViewType = DevExpress.XtraScheduler.SchedulerViewType.Day
    End Sub

    Private Sub btnSemanal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSemanal.Click
        calendarioConsultas.ActiveViewType = DevExpress.XtraScheduler.SchedulerViewType.WorkWeek
    End Sub

    Private Sub btnMensual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMensual.Click
        calendarioConsultas.ActiveViewType = DevExpress.XtraScheduler.SchedulerViewType.Month
    End Sub

    Private Sub btnHistorico_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHistorico.Click
        calendarioConsultas.ActiveViewType = DevExpress.XtraScheduler.SchedulerViewType.Timeline
    End Sub

    Private Sub calendarioConsultas_PreparePopupMenu(ByVal sender As Object, ByVal e As DevExpress.XtraScheduler.PreparePopupMenuEventArgs) Handles calendarioConsultas.PreparePopupMenu
        e.Menu.Items.Clear()
    End Sub

    Private Sub VisualizarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VisualizarToolStripMenuItem.Click
        If calendarioConsultas.SelectedAppointments.Count > 0 Then
            frmConsulta.habilitarVisualización()
            Dim ConsultaFarmaciaId As Integer = calendarioConsultas.SelectedAppointments(0).GetValue(calendarioConsultas.Storage, "ConsultaFarmaciaId")
            frmConsulta.CargarConsulta(ConsultaFarmaciaId)
            frmConsulta.ShowDialog()
        End If
    End Sub

    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        If calendarioConsultas.SelectedAppointments.Count > 0 Then
            frmConsulta.habilitarMoficacion()
            Dim ConsultaFarmaciaId As Integer = calendarioConsultas.SelectedAppointments(0).GetValue(calendarioConsultas.Storage, "ConsultaFarmaciaId")
            frmConsulta.CargarConsulta(ConsultaFarmaciaId)
            frmConsulta.ShowDialog()
        End If
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        If MsgBox("Desea eliminar la columna seleccionada y su solución?", MsgBoxStyle.Exclamation, "Elimincación...") Then
            Dim ConsultaFarmaciaId As Integer = calendarioConsultas.SelectedAppointments(0).GetValue(calendarioConsultas.Storage, "ConsultaFarmaciaId")
            ConsultaFarmaciaDAO.eliminarConsultaFarmacia(ConsultaFarmaciaId)
            actualizarCalendario()
        End If
    End Sub

    Private Sub AgregarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AgregarToolStripMenuItem.Click
        If calendarioConsultas.SelectedAppointments.Count > 0 Then
            frmConsulta.habilitarCarga()
            frmConsulta.ShowDialog()
        End If
    End Sub
End Class